package com.star.tool.widget;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.star.tool.R;

/**
 * Author: blackbai11
 * Date: 2018/5/31
 * Description: TitleBar一般通用标题栏
 */
public class TitleBar extends RelativeLayout {

    /**
     * 默认值（根据UI调整）
     */
    private int TITLE_SIZE = 18;//标题文本大小
    private int BUTTON_TEXT_SIZE = 16;//左右按钮文本大小
    private int BUTTON_IMG_WIDTH = 12;//左右按钮图标宽度
    private int BUTTON_IMG_HEIGHT = 24;//左右按钮图标高度
    private int TEXT_COLOR = getResources().getColor(R.color.titleColor);//文本默认颜色（黑0xff000000 注：必须带透明度的8位）
    private boolean DEFAULT_BACK = true;//默认返回
    private int DEFAULT_BACK_ICON = R.mipmap.icon_back;//默认返回图标
    private int BG_COLOR = R.color.colorPrimary;//默认背景色

    private Context mContext;
    private TextView mTitleView, mLeftButton, mrRightButton;

    public TitleBar(final Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.layout_title_bar, this);
        mTitleView = findViewById(R.id.titleView);
        mLeftButton = findViewById(R.id.leftButton);
        mrRightButton = findViewById(R.id.rightButton);
        /**
         * 获取自定义属性
         */
        TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBar);
        //标题
        String titleText = typeArray.getString(R.styleable.TitleBar_titleText);
        float titleSize = typeArray.getDimension(R.styleable.TitleBar_titleSize, dp2Px(context, TITLE_SIZE));
        int titleColor = typeArray.getColor(R.styleable.TitleBar_titleColor, TEXT_COLOR);
        //左边按钮
        String leftText = typeArray.getString(R.styleable.TitleBar_leftText);
        float leftTextSize = typeArray.getDimension(R.styleable.TitleBar_leftTextSize, dp2Px(context, BUTTON_TEXT_SIZE));
        int leftTextColor = typeArray.getColor(R.styleable.TitleBar_leftTextColor, TEXT_COLOR);
        int leftImg = typeArray.getResourceId(R.styleable.TitleBar_leftImg, 0);
        float leftImgWidth = typeArray.getDimension(R.styleable.TitleBar_leftImgWidth, dp2Px(context, BUTTON_IMG_WIDTH));
        float leftImgHeight = typeArray.getDimension(R.styleable.TitleBar_leftImgHeight, dp2Px(context, BUTTON_IMG_HEIGHT));
        //右边按钮
        String rightText = typeArray.getString(R.styleable.TitleBar_rightText);
        float rightTextSize = typeArray.getDimension(R.styleable.TitleBar_rightTextSize, dp2Px(context, BUTTON_TEXT_SIZE));
        int rightTextColor = typeArray.getColor(R.styleable.TitleBar_rightTextColor, TEXT_COLOR);
        int rightImg = typeArray.getResourceId(R.styleable.TitleBar_rightImg, 0);
        float rightImgWidth = typeArray.getDimension(R.styleable.TitleBar_rightImgWidth, dp2Px(context, BUTTON_IMG_WIDTH));
        float rightImgHeight = typeArray.getDimension(R.styleable.TitleBar_rightImgHeight, dp2Px(context, BUTTON_IMG_HEIGHT));
        //释放资源
        typeArray.recycle();
        /**
         * 自定义属性赋值
         */
        //标题
        mTitleView.setText(titleText);
        mTitleView.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
        mTitleView.setTextColor(titleColor);
        //左边按钮
        mLeftButton.setText(leftText);
        mLeftButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, leftTextSize);
        mLeftButton.setTextColor(leftTextColor);
        if (leftImg != 0) {
            Drawable leftDrawable = getResources().getDrawable(leftImg);
            leftDrawable.setBounds(0, 0, (int) leftImgWidth, (int) leftImgHeight);
            mLeftButton.setCompoundDrawables(leftDrawable, null, null, null);
        }
        //右边按钮
        mrRightButton.setText(rightText);
        mrRightButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, rightTextSize);
        mrRightButton.setTextColor(rightTextColor);
        if (rightImg != 0) {
            Drawable rightDrawable = getResources().getDrawable(rightImg);
            rightDrawable.setBounds(0, 0, (int) rightImgWidth, (int) rightImgHeight);
            mrRightButton.setCompoundDrawables(rightDrawable, null, null, null);
        }
        //默认返回
        setDefaultBack(DEFAULT_BACK);
        setBgColor(getResources().getColor(BG_COLOR));
    }

    /**
     * dp转px
     */
    private int dp2Px(Context context, float dp) {
        return (int) (dp * context.getResources().getDisplayMetrics().density + 0.5f);
    }

    /**
     * 通过 View 暴力获取Activity
     */
    public static Activity getActivity(View view) {
        Context context = view.getContext();
        while (context instanceof ContextWrapper) {
            if (context instanceof Activity) {
                return (Activity) context;
            }
            context = ((ContextWrapper) context).getBaseContext();
        }
        return (Activity) view.getRootView().getContext();
    }

    /**
     * 标题属性设置
     */
    //设置标题文字
    public void setTitle(String title) {
        mTitleView.setText(title);
    }

    //设置标题大小
    public void setTitleSize(float sp) {
        mTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, sp);
    }

    //设置标题颜色
    public void setTitleColor(int color) {
        mTitleView.setTextColor(color);
    }

    /**
     * 左按钮属性设置
     */
    //设置左文本
    public void setLeftText(String title) {
        mLeftButton.setText(title);
    }

    //设置左文本大小
    public void setLeftTextSize(float sp) {
        mLeftButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, sp);
    }

    //设置左文本颜色
    public void setLeftTextColor(int color) {
        mLeftButton.setTextColor(color);
    }

    //设置左图标
    public void setLeftImg(int img) {
        Drawable drawable = null;
        if (img != 0) {
            drawable = getResources().getDrawable(img);
            drawable.setBounds(0, 0, dp2Px(mContext, BUTTON_IMG_WIDTH), dp2Px(mContext, BUTTON_IMG_HEIGHT));
            return;
        }
        mLeftButton.setCompoundDrawables(drawable, null, null, null);
    }

    //设置左图标及大小
    public void setLeftImg(int img, int width, int height) {
        Drawable drawable = null;
        if (img != 0) {
            drawable = getResources().getDrawable(img);
            drawable.setBounds(0, 0, dp2Px(mContext, width), dp2Px(mContext, height));
            return;
        }
        mLeftButton.setCompoundDrawables(drawable, null, null, null);
    }

    //设置左边按钮的可见性
    public void setLeftVisibility(int visibility) {
        mLeftButton.setVisibility(visibility);
    }

    //设置左按钮点击事件
    public void setLeftClickListener(OnClickListener l) {
        mLeftButton.setOnClickListener(l);
    }

    //左按钮设置是否默认返回
    public void setDefaultBack(boolean defaultBack) {
        DEFAULT_BACK = defaultBack;
        //默认返回键
        if (DEFAULT_BACK) {
            Drawable leftDrawable = getResources().getDrawable(DEFAULT_BACK_ICON);
            leftDrawable.setBounds(0, 0, dp2Px(mContext, BUTTON_IMG_WIDTH), dp2Px(mContext, BUTTON_IMG_HEIGHT));
            mLeftButton.setCompoundDrawables(leftDrawable, null, null, null);
            setLeftClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    getActivity(v).onBackPressed();
                }
            });
        } else {
            mLeftButton.setCompoundDrawables(null, null, null, null);
            setLeftClickListener(null);
        }
    }

    /**
     * 右按钮属性设置
     */
    //设置右文本
    public void setRightText(String title) {
        mrRightButton.setText(title);
    }

    //设置右文本大小
    public void setRightTextSize(float sp) {
        mrRightButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, sp);
    }

    //设置右文本颜色
    public void setRightTextColor(int color) {
        mrRightButton.setTextColor(color);
    }

    //设置右图标
    public void setRightImg(int img) {
        Drawable drawable = null;
        if (img != 0) {
            drawable = getResources().getDrawable(img);
            drawable.setBounds(0, 0, dp2Px(mContext, BUTTON_IMG_WIDTH), dp2Px(mContext, BUTTON_IMG_HEIGHT));
            return;
        }
        mrRightButton.setCompoundDrawables(drawable, null, null, null);
    }

    //设置右图标及大小
    public void setRightImg(int img, int width, int height) {
        Drawable drawable = null;
        if (img != 0) {
            drawable = getResources().getDrawable(img);
            drawable.setBounds(0, 0, dp2Px(mContext, width), dp2Px(mContext, height));
            return;
        }
        mrRightButton.setCompoundDrawables(drawable, null, null, null);
    }

    //设置右边按钮的可见性
    public void setRightVisibility(int visibility) {
        mrRightButton.setVisibility(visibility);
    }

    //设置右按钮点击事件
    public void setRightClickListener(OnClickListener l) {
        mrRightButton.setOnClickListener(l);
    }

    //设置TitleBar背景色
    public void setBgColor(int color) {
        setBackgroundColor(color);
    }

}
